package com.example.yiwei.crm.view.activity.contactActivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.entity.ContactMessageEntity;

import java.util.List;

public class ContactSearchActivity extends AppCompatActivity {
    EditText edit_search;
    Button btn_search;
    ListView listView;
    List<ContactMessageEntity> contactList;
    MyAdapter adapter;
    customerDao customerdao;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_search);
        edit_search= (EditText) findViewById(R.id.contact_edit_search);
        btn_search= (Button) findViewById(R.id.contact_btn_search);
        listView= (ListView) findViewById(R.id.contact_listview_search);
        edit_search= (EditText) findViewById(R.id.contact_edit_search);
        adapter=new MyAdapter();
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        customerdao=new customerDaoImpl();
                        contactList=customerdao.queryContactAllMessage(baseClass.contact_search_url+edit_search.getText().toString(),null);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (contactList.size()==0){
                                    Toast.makeText(ContactSearchActivity.this, "联系人不存在!", Toast.LENGTH_SHORT).show();
                                }
                                listView.setAdapter(adapter);
                                adapter.notifyDataSetChanged();
                                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                        final int contact_id=contactList.get(i).getContact_id();
                                        intent=new Intent(ContactSearchActivity.this,ContactDetaledMessageActivity.class);
                                        intent.putExtra("id",contact_id);
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                customerDao customerdao=new customerDaoImpl();
                                                GetContact.contact= customerdao.queryContactMessage(baseClass.query_contact_message_url+contact_id, null);
                                                if ("success".equals(GetContact.contact.getTag())){
                                                    startActivity(intent);
                                                    finish();
                                                }else{
                                                    runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            Toast.makeText(ContactSearchActivity.this, "数据加载失败", Toast.LENGTH_SHORT).show();
                                                        }
                                                    });
                                                }

                                            }
                                        }).start();
                                    }
                                });

                            }
                        });
                    }
                }).start();
            }
        });

    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return   contactList.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view==null){
                view=getLayoutInflater().inflate(R.layout.listview_contact,null);
                holder=new ViewHolder();
                holder.txt_name= (TextView) view.findViewById(R.id.listview_contact_txt_name);
                holder.txt_phone= (TextView) view.findViewById(R.id.listview_contact_txt_mobile_phone);
                view.setTag(holder);
            }else{
                holder= (ViewHolder) view.getTag();
            }
            holder.txt_name.setText(contactList.get(i).getContact_name());
            holder.txt_phone.setText(contactList.get(i).getContact_mobile_phone());
            return view;
        }
        class ViewHolder{
            TextView txt_name,txt_phone;
        }
    }
}
